<?php

namespace Budget\BudgetBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Budget\BudgetBundle\Entity\Family;
use Budget\BudgetBundle\Entity\PlannedExpense;

class PlannedExpenseType extends AbstractType
{
    
    protected $userFamily;
    protected $expense;
    
    public function __construct(Family $userFamily, PlannedExpense $expense = null)
    {
        $this->userFamily = $userFamily;
        $this->expense    = $expense;
    }
    
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $userFamily = $this->userFamily;
        
        $builder
            ->add('amount', 'money', array('label' => 'Amount:'))
            ->add('category', 'entity', array(
                'class' => 'BudgetBudgetBundle:Category',
                'query_builder' => function(EntityRepository $er) use($userFamily) {
                    return $er->createQueryBuilder('p')
                        ->where('p.family = :family')
                        ->andWhere('p.date_deleted IS NULL')
                        ->setParameter('family', $userFamily->getId())
                        ->orderBy('p.name', 'ASC');
                },
                'label'    => 'Category:',
                'multiple' => false
            ))
            ->add('name', null, array('label' => 'Name:'))   
            ->add('date_expired', 'collot_datetime', array(
                'pickerOptions'  => array(
                    'format'         => 'dd.mm.yyyy',
                    'pickerPosition' => 'bottom-left',
                    'minView'        => 'month',
                ),
                'label' => 'Date expired:'));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'planned_expense';
    }
    
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Budget\BudgetBundle\Entity\PlannedExpense',
        ));
    }

}
